package tech.abing.spider.task.pageprocessor.amac;

import java.sql.Timestamp;
import java.util.List;
import java.util.UUID;
import java.util.regex.Pattern;

import org.apache.commons.lang3.time.DateUtils;
import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import tech.abing.spider.component.PageProcessor;
import tech.abing.spider.component.PageScript;
import tech.abing.spider.model.Proccessable;
import tech.abing.spider.page.OkPage;
import tech.abing.spider.page.Page;
import tech.abing.spider.request.BasicRequest;
import tech.abing.spider.request.PageRequest;
import tech.abing.spider.request.PageRequest.Method;
import tech.abing.spider.request.StartContext;
import tech.abing.spider.task.model.amac.LostPrivPlaceOrgHtmlEntity;
import tech.abing.spider.util.StringUtils;

public class LostPrivPlaceOrgProcessor implements PageProcessor {

	private static Logger logger = Logger.getLogger(LostPrivPlaceOrgProcessor.class);

	private static int record = 0;

	@Override
	public PageScript getJavaScript() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Pattern getNormalContain() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void process(OkPage page, StartContext context, List<BasicRequest> queue, List<Proccessable> objectContainer) throws Exception {
		Document doc = Jsoup.parse(page.getContent());
		PageRequest request = page.getRequest();
		if (request.getUrl().contains("http://www.amac.org.cn/xhdt/zxdt/index")) {
			if (request.getUrl().contains("index.shtml")) {
				for (int i = 1; i <= 50; i++) {
					String url = request.getUrl().replace("index.shtml", "index_" + i + ".shtml");
					PageRequest req = context.createPageRequest(url, LostPrivPlaceOrgProcessor.class);

					req.setMethod(request.getMethod());
					req.setPostType(request.getPostType());
					req.setReqPayloadParams(request.getReqPayloadParams());
					req.setHeaders(request.getHeaders());
					req.setRequestParams(request.getRequestParams());
					queue.add(req);
				}
			}

			Element content = doc.select(".iRight").first();
			for (int i = 0; i >= 0; i++) {
				Element div;
				try {
					div = content.child(i);
				} catch (Exception e) {
					break;
				}
				div = div.select("div:eq(" + i + ") > .newsName > a").first();
				if (null == div) {
					continue;
				}
				String title = div.text().replaceAll("\\(", "（").replaceAll("\\)", "）");
				if (title.contains("失联私募机构的公告") || title.contains("失联（异常）私募机构的公告")) {
					String url = "http://www.amac.org.cn/" + div.attr("href").replaceAll("../../", "");
					PageRequest req = context.createPageRequest(url, LostPrivPlaceOrgProcessor.class);
					req.setMethod(Method.GET);
					req.setPostType(request.getPostType());
					req.setReqPayloadParams("{}");
					req.putHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
					req.putHeader("Accept-Encoding", "gzip, deflate, sdch");
					req.putHeader("Accept-Language", "zh-CN,zh;q=0.8,en;q=0.6");
					req.putHeader("Cache-Control", "max-age=0");
					req.putHeader("Connection", "keep-alive");
					req.putHeader("Host", "www.amac.org.cn");
					req.putHeader("Referer", "http://www.amac.org.cn/xhdt/zxdt/");
					req.putHeader("Upgrade-Insecure-Requests:", "1");
					req.putHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36");
					queue.add(req);
				}
			}
		} else {
			LostPrivPlaceOrgHtmlEntity entity = new LostPrivPlaceOrgHtmlEntity();
			entity.setId(UUID.randomUUID().toString());
			entity.setCreateTime(new Timestamp(System.currentTimeMillis()));
			entity.setUrl(page.getRequest().getUrl());
			Element content = doc.select(".iRight").first();
			entity.setTitle(content.select(".ldT").first().text());
			try {
				String dateStr = content.select(".ldDate").first().text().trim();
				entity.setDate(DateUtils.parseDate(dateStr.substring(dateStr.length() - 10), "yyyy-MM-dd"));
			} catch (Exception e) {
				e.printStackTrace();
			}
			entity.setContent(content.select(".ldContent").first().toString());
			StringUtils.removeSpace(entity);
			objectContainer.add(entity);
		}
	}

	@Override
	public void processErrorPage(Page page, StartContext context, List<Proccessable> objectContainer) throws Exception {
		// TODO Auto-generated method stub

	}

}